import { Transform, Type } from 'class-transformer';
import {
  IsEnum,
  IsNotEmpty,
  IsOptional,
  IsString,
  ValidateNested,
} from 'class-validator';

import { IndexSorts } from '../../types';

class SortDto<K> {
  @IsOptional()
  @IsString()
  @IsNotEmpty()
  orderField: K;

  @IsOptional()
  @IsEnum(['ASC', 'DESC'], { message: 'orderType 必须是 ASC 或 DESC' })
  @Transform(({ value }) => {
    if (typeof value === 'string' && value.trim() !== '') {
      return value.toUpperCase() as IndexSorts['orderType'];
    }
    return value;
  })
  orderType: IndexSorts['orderType'];
}

export class IndexSortDto<T> {
  @ValidateNested()
  @IsOptional()
  @Type(() => SortDto)
  sorts?: SortDto<T>[];
}
